<template>
  <div class="app-container" v-loading="formLoading">
    <el-form ref="ruleForm" :rules="rules" :model="form" label-width="100px">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username"/>
      </el-form-item>
      <el-form-item label="账号" prop="account">
        <el-input v-model="form.account"/>
        <p style="margin: 0;">新用户默认密码：123456</p>
      </el-form-item>
      <el-form-item label="电话号码" prop="mobilephone">
        <el-input v-model="form.mobilephone"/>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio v-model="form.sex" :label="1">男</el-radio>
        <el-radio v-model="form.sex" :label="0">女</el-radio>
      </el-form-item>
      <!-- <el-form-item label="生日" prop="birth">
        <el-date-picker v-model="form.birth2" @change="getDate" type="date"  placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="职位" prop="postName">
        <el-input v-model="form.postName"/>
      </el-form-item> -->
      <!-- <el-form-item label="区域" prop="areaName">
        <v-distpicker  @province="onChangeProvince" @city="onChangeCity" @area="onChangeArea"></v-distpicker>
      </el-form-item> -->
      <el-form-item label="角色" prop="roleId">
        <el-select v-model="form.roleId" filterable placeholder="请选择角色" style="width: 50%;">
          <el-option v-for="(item, index) in roleList" :value-key="index" :label="item.roleName" :value="item.id" />
        </el-select>
      </el-form-item>
    </el-form>
    <div style="text-align: center;">
      <el-button type="primary" @click="onSubmit('ruleForm')" :loading="btnLoading">添加</el-button>
      <el-button @click="onCancel">关闭</el-button>
    </div>
  </div>
</template>

<script>
import request from '@/utils/request'
import VDistpicker from 'v-distpicker'
import crypto from 'crypto'

export default {
  components: { VDistpicker },
  props: ["id"],

  data() {
    var validatorPhone = function (rule, value, callback) {
      if (value == undefined) {
        callback(new Error('手机号不能为空'))
      } else if (!/^1\d{10}$/.test(value)) {
        callback(new Error('手机号格式错误'))
      } else {
        callback()
      }
    }
    return {
      institutionList: [],
      tipShow:false,
      roleList: [],
      deptmentList:[],
      form: {
        sex: 1
      },
      buttonLoading: false,
      formLoading: true,
      btnLoading: false,
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
        ],
        mobilephone: [
          { required: true, validator: validatorPhone, trigger: 'blur' }
        ],
        roleId: [
          { required: true, message: '请选择角色', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    // this.getRoleList();
    // this.getInstitutionList();
  },
  mounted() {
      this.getRoleList();
  },
  methods: {
    //获取所有角色信息展示在下拉框中
    getRoleList() {
      this.$axios({
        method:'get',
        url:'/system/role/list',
      }).then((res) => {
        this.roleList = res.data;
        this.formLoading = false;
      })
    },
    getInstitutionList() {
      request({
        url: '/institution/getInstitutionList',
        method: 'get',
        params: {}
      }).then((res) => {
        this.listLoading = true
        this.institutionList = res.result;
        this.listLoading = false
      }).catch((err) => {
        this.$message(err)
      })
    },
    onSubmit(ruleForm) {
      this.form.deptId = this.id;
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          this.btnLoading = true;
          this.$axios({
            url: '/system/user/addUser',
            method: 'post',
            data: this.$qs.stringify(this.form)
          }).then((res) => {
            if (res.code == 200) {
              this.$message.success('新增人员成功')
              this.$emit('closeDialog', false);
              this.$emit('refresh');
              this.btnLoading = false;
            } else{
              this.$message.warning('修改人员失败')
            }
          }).catch((err) => {
            this.btnLoading = false;
            this.$message(err)
          })
        } else {
          this.$message({
            message: '表单验证失败',
            type: 'warning'
          })

          return false;
        }
      });
    },
    //获取省市区信息赋值给form
    onChangeProvince(a){
      this.form.areaName = a.value;
      this.form.areaId = a.code;
    },
    onChangeCity(a){
      this.form.areaName = this.form.areaName + a.value;
      this.form.areaId = a.code;
    },
    onChangeArea(a){
      this.form.areaName = this.form.areaName + a.value;
      this.form.areaId = a.code;
    },
    ////////////////
    onCancel() {
      this.$emit('closeDialog', false);
    },
    getDate (date){
      this.form.birth = date;
    }
  }
}
</script>

<style scoped>
/*.app-container{
  width:400px;
}*/
.line{
  text-align: center;
}
.el-input{
  width: 86%;
}


</style>
